<?php
/**
 * 飞信机器人--功能插件--全文翻译
 * 功能：实现英文词典功能
 * 数据表：无
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
class FDict extends CommonPlugin
{
	var $UUID='92d07692-47c6-48f9-a361-dc2d88195417';

	public function FDict(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	function getTips()
	{
		return "请输入需要翻译的中英文短句\n".$this->getFooter();
	}
	
	function isEnglish($str)
	{
		for($i=0; $i<strlen($str); $i++)
		{
			if( ord($str[$i]) >= 128 )
				return false;
		}
		return true;
	}
	
	function handle($userInput)
	{
		if( parent::handleInput($input, $shortcut)) return false;
		
		$str = iconv('UTF-8','GBK', $userInput);
		if( !$this->isEnglish($str) )
		{
			$url = "http://www.google.com/uds/Gtranslate?callback=google.language.callbacks.id102&context=22&q={translate}&langpair=zh%7Cen&key=notsupplied&v=1.0&nocache=1226926245757";
		}else
		{
			$url = "http://www.google.com/uds/Gtranslate?callback=google.language.callbacks.id103&context=22&q={translate}&langpair=en%7Czh-CN&key=notsupplied&v=1.0&nocache=1226926245758";
		}
		$url = str_replace('{translate}', urlencode($userInput), $url);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		
		ob_start(); 
		curl_exec($ch);
		$content = ob_get_contents();
		ob_end_clean();
		
		curl_close($ch);

		$start = "\"translatedText\":\"";
		$end  = "\"";

		$posStart = strpos($content, $start)+strlen($start);
		$posEnd = strpos($content, $end, $posStart);

		$tmpStr = substr($content, $posStart, $posEnd-$posStart);

		$tmpStr.= "\n请输入需要翻译的中英文短句";
		return $tmpStr.$this->getFooter();
	}	
	
}

?>
